import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JTabbedPane;
import javax.swing.border.SoftBevelBorder;
import javax.swing.border.BevelBorder;
import javax.swing.JList;
import java.awt.SystemColor;
import java.util.ArrayList;

import javax.swing.AbstractListModel;
import javax.swing.JButton;
import javax.swing.ListSelectionModel;
import javax.swing.JScrollPane;
import java.awt.Font;
import java.awt.Window.Type;
import javax.swing.UIManager;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;


public class CommandGUI
{
	private JFrame frmSystem;
	CommandControl objControl = new CommandControl();
	ArrayList processes = new ArrayList();

	public static void main(String[] args)
	{
		EventQueue.invokeLater(new Runnable()
		{
			public void run()
			{
				try
				{
					CommandGUI window = new CommandGUI();
					window.frmSystem.setVisible(true);
				} 
				catch (Exception e)
				{
					e.printStackTrace();
				}
			}
		});
	}

	public CommandGUI()
	{
		initialize();
	}
	
	public ArrayList getProcesses()
	{ 
		objControl.inputCommand("cmd /c tasklist", true);
		return objControl.output;
	}
	
	public void endProcess(String str, JList list)
	{
		objControl.inputCommand("cmd /c taskkill /im "+str, false);
		refreshProcesses(list);
	}
	
	public void refreshProcesses(JList list)
	{
		list.setListData(getProcesses().toArray());
	}

	private void initialize()
	{
		frmSystem = new JFrame();
		frmSystem.setBackground(UIManager.getColor("Button.background"));
		frmSystem.setForeground(SystemColor.controlDkShadow);
		frmSystem.setType(Type.UTILITY);
		frmSystem.setTitle("System");
		frmSystem.setBounds(100, 100, 420, 325);
		frmSystem.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmSystem.getContentPane().setLayout(null);
		
		final JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		tabbedPane.setBounds(0, 0, 404, 253);
		tabbedPane.setBorder(new SoftBevelBorder(BevelBorder.RAISED, null, null, null, null));
		frmSystem.getContentPane().add(tabbedPane);
		
		ArrayList temp = getProcesses();
		
		final JList list = new JList(temp.toArray());
		list.setFont(new Font("Source Code Pro Light", Font.PLAIN, 14));
		list.setValueIsAdjusting(true);
		list.setBackground(SystemColor.menu);
		list.setVisibleRowCount(3);
		list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		/*
		 */
		
		final JScrollPane scrollPane = new JScrollPane(list);
		
		tabbedPane.addTab("Processes", null, scrollPane, null);
		tabbedPane.setEnabledAt(0, true);
		//tabbedPane.addTab("Processes", null, list, null);
		tabbedPane.setBackgroundAt(0, SystemColor.activeCaption);
		tabbedPane.setForegroundAt(0, Color.LIGHT_GRAY);
		
		JScrollPane scrollPane_1 = new JScrollPane();
		tabbedPane.addTab("New tab", null, scrollPane_1, null);
		
		JButton button = new JButton("End Process");
		button.setBounds(23, 252, 111, 23);
		button.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent arg0) 
			{
				if (list.getSelectedIndex() > 2)
				{
					String tempStr = list.getSelectedValue().toString().toLowerCase();
					int endPoint = tempStr.length();
					if (tempStr.contains(".exe"))
					{
						endPoint = tempStr.indexOf(".exe")+4;
					}
					String str = tempStr.substring(0, endPoint);
					endProcess(str, list);
					System.out.println(str);
				}
			}
			
		});
		frmSystem.getContentPane().add(button);
		
		JButton btnRefresh = new JButton("Refresh");
		btnRefresh.setBounds(144, 252, 111, 23);
		btnRefresh.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent e) 
			{
				refreshProcesses(list);
			}
		});
		frmSystem.getContentPane().add(btnRefresh);
	}
}
